﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace AppCommon
{
    /// <summary>
    /// 日志操作类
    /// </summary>
    public class LogManager
    {
        private static int logLevel = 0;
        private static StorageFile logPath = null;
        private static LogManager manager = new LogManager();
        private static StorageFolder path = null;



        private static int debugLevel = 0;
        private static int infoLevel = 1;
        private static int warnLevel = 2;
        private static int errorLevel = 3;
        private static int fatalLevel = 4;

        private LogManager() { }

        private async static Task<int> GetRoot()
        {
            string filePath = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day;
            logPath = await path.CreateFileAsync(filePath + ".txt", CreationCollisionOption.OpenIfExists);
            return 1;
        }

        public async static Task<LogManager> GetInstrance()
        {
            if (logPath == null)
            {
                var root = Windows.Storage.ApplicationData.Current.LocalFolder;
                path = await root.CreateFolderAsync("Log", CreationCollisionOption.OpenIfExists);
            }
            await GetRoot();
            return manager;
        }

        public async Task<int> Debug(string message)
        {
            if (debugLevel >= logLevel)
            {
                string messageString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    <debug>    " + message;
                List<string> messages = new List<string>();
                messages.Add(messageString);
                await Windows.Storage.FileIO.AppendLinesAsync(logPath, messages, Windows.Storage.Streams.UnicodeEncoding.Utf8);
                messages = null;
            }
            return 1;
        }

        public async Task<int> Info(string message)
        {
            if (infoLevel >= logLevel)
            {
                string messageString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    <info>     " + message;
                List<string> messages = new List<string>();
                messages.Add(messageString);
                await Windows.Storage.FileIO.AppendLinesAsync(logPath, messages, Windows.Storage.Streams.UnicodeEncoding.Utf8);
                messages = null;
            }
            return 1;
        }

        public async Task<int> Warn(string message)
        {
            if (warnLevel >= logLevel)
            {
                string messageString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    <warn>     " + message;
                List<string> messages = new List<string>();
                messages.Add(messageString);
                await Windows.Storage.FileIO.AppendLinesAsync(logPath, messages, Windows.Storage.Streams.UnicodeEncoding.Utf8);
                messages = null;
            }
            return 1;
        }

        public async Task<int> Error(string message)
        {
            if (errorLevel >= logLevel)
            {
                string messageString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    <error>    " + message;
                List<string> messages = new List<string>();
                messages.Add(messageString);
                await Windows.Storage.FileIO.AppendLinesAsync(logPath, messages, Windows.Storage.Streams.UnicodeEncoding.Utf8);
                messages = null;
            }
            return 1;
        }

        public async Task<int> Fatal(string message)
        {
            if (fatalLevel >= logLevel)
            {
                string messageString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    <fatal>    " + message;
                List<string> messages = new List<string>();
                messages.Add(messageString);
                await Windows.Storage.FileIO.AppendLinesAsync(logPath, messages, Windows.Storage.Streams.UnicodeEncoding.Utf8);
                messages = null;
            }
            return 1;
        }
    }
}
